package dao

import (
	"haiqiu-go-admin/entity"
	"haiqiu-go-admin/entity/dto"
	"haiqiu-go-admin/utils"
	"log"
	"strconv"
	"strings"
)

// DeptById 根据ID查询数据
func DeptById(id int64) (*entity.SysDept, error) {
	row := DB.QueryRow("select * from sys_dept where id = ?", id)
	var sysDept entity.SysDept
	err := row.Scan(
		&sysDept.Id,
		&sysDept.DeptName,
		&sysDept.Cover,
		&sysDept.Leader,
		&sysDept.Phone,
		&sysDept.Email,
		&sysDept.ParentId,
		&sysDept.Desc,
		&sysDept.Sort,
		&sysDept.Status,
		&sysDept.IsDelete,
		&sysDept.CreateTime,
		&sysDept.UpdateTime,
	)
	if err != nil && err.Error() != utils.NoSet {
		panic(err)
	}
	return &sysDept, nil
}

// DeptPageCount 分页查询总条数
func DeptPageCount() (count int64) {
	err := DB.QueryRow("select count(1) from sys_dept").Scan(&count)
	if err != nil {
		log.Println(err.Error())
	}
	return count
}

// DeptPageList 分页查询数据
func DeptPageList(page, pageSize int64) (*[]entity.SysDept, error) {
	rows, err := DB.Query("select * from sys_dept limit ?,?", page, pageSize)
	if err != nil {
		log.Println(err.Error())
		return nil, err
	}
	var sysDepts []entity.SysDept
	for rows.Next() {
		var sysDept = entity.SysDept{}
		err := rows.Scan(
			&sysDept.Id,
			&sysDept.DeptName,
			&sysDept.Cover,
			&sysDept.Leader,
			&sysDept.Phone,
			&sysDept.Email,
			&sysDept.ParentId,
			&sysDept.Desc,
			&sysDept.Sort,
			&sysDept.Status,
			&sysDept.IsDelete,
			&sysDept.CreateTime,
			&sysDept.UpdateTime,
		)
		if err != nil {
			log.Println(err.Error())
			return nil, err
		}
		sysDepts = append(sysDepts, sysDept)
	}
	return &sysDepts, nil
}

// DeptAdd 新增数据
func DeptAdd(sysDept *entity.SysDept) (int64, error) {
	exec, err := DB.Exec("INSERT INTO `sys_dept` "+
		"(`id`, `dept_name`, `cover`, `leader`, `phone`, `email`, `parent_id`, "+
		"`desc`, `sort`,`status`,`is_delete`,`create_time`, `update_time`) VALUES "+
		"(?,?,?,?,?,?,?,?,?,?,?,?,?);",
		&sysDept.Id,
		&sysDept.DeptName,
		&sysDept.Cover,
		&sysDept.Leader,
		&sysDept.Phone,
		&sysDept.Email,
		&sysDept.ParentId,
		&sysDept.Desc,
		&sysDept.Sort,
		&sysDept.Status,
		&sysDept.IsDelete,
		&sysDept.CreateTime,
		&sysDept.UpdateTime,
	)
	if err != nil {
		return 0, err
	}
	rowsAffected, err := exec.RowsAffected()
	if err != nil {
		return 0, err
	}
	return rowsAffected, nil
}

// DeptUpdate 根据ID更新数据
func DeptUpdate(sysDept *entity.SysDept) (int64, error) {
	exec, err := DB.Exec("UPDATE `sys_dept` SET `dept_name` = '?', `cover` = '?',"+
		" `leader` = '?', `phone` = '?', `email` = '?', `parent_id` = ?, `desc` = '?',"+
		" `sort` = ?, `status` = ?, `is_delete` = ?, `create_time` = '?', `update_time` = '?' WHERE `id` = ?;",
		&sysDept.DeptName,
		&sysDept.Cover,
		&sysDept.Leader,
		&sysDept.Phone,
		&sysDept.Email,
		&sysDept.ParentId,
		&sysDept.Desc,
		&sysDept.Sort,
		&sysDept.Status,
		&sysDept.IsDelete,
		&sysDept.CreateTime,
		&sysDept.UpdateTime,
		&sysDept.Id,
	)
	if err != nil {
		return 0, err
	}
	rowsAffected, err := exec.RowsAffected()
	if err != nil {
		return 0, err
	}
	return rowsAffected, nil
}

// DeptDelete 根据ID删除数据
func DeptDelete(id int64) (int64, error) {
	exec, err := DB.Exec("delete from sys_dept where id = ?")
	if err != nil {
		panic(err)
		return 0, err
	}
	rowsAffected, err := exec.RowsAffected()
	if err != nil {
		panic(err)
		return 0, err
	}
	return rowsAffected, nil
}

// DeptDeleteBatch 根据ID批量删除数据
func DeptDeleteBatch(ids []int64) (int64, error) {
	var idsStr string
	for index, id := range ids {
		if index == len(ids)-1 {
			idsStr += strconv.FormatInt(id, 10)
		} else {
			idsStr += strconv.FormatInt(id, 10) + ", "
		}
	}
	exec, err := DB.Exec("delete from sys_dept where id in (?)", idsStr)
	if err != nil {
		panic(err)
		return 0, err
	}
	rowsAffected, err := exec.RowsAffected()
	if err != nil {
		panic(err)
		return 0, err
	}
	return rowsAffected, nil
}

// DeptList 查询所有数据
func DeptList(deptQueryDto dto.DeptQueryDto) (*[]entity.SysDept, error) {
	wrapper := utils.QueryWrapper().All("sys_dept")
	if deptQueryDto.Params.DeptName != nil {
		wrapper.Like("dept_name", *deptQueryDto.Params.DeptName)
	}
	if deptQueryDto.Params.Status != nil {
		wrapper.Eq("status", *deptQueryDto.Params.Status)
	}
	rows, err := DB.Query(wrapper.GetSql(), wrapper.GetVal()...)
	if err != nil {
		panic(err)
		return nil, err
	}
	sysDepts := make([]entity.SysDept, 0)
	for rows.Next() {
		var sysDept entity.SysDept
		err := rows.Scan(
			&sysDept.Id,
			&sysDept.DeptName,
			&sysDept.Cover,
			&sysDept.Leader,
			&sysDept.Phone,
			&sysDept.Email,
			&sysDept.ParentId,
			&sysDept.Desc,
			&sysDept.Sort,
			&sysDept.Status,
			&sysDept.IsDelete,
			&sysDept.CreateTime,
			&sysDept.UpdateTime,
		)
		if err != nil {
			panic(err)
			return nil, err
		}
		sysDepts = append(sysDepts, sysDept)
	}
	return &sysDepts, nil
}

func ActiveDeptById(sysDepts []entity.SysDept) (any, error) {
	var ids strings.Builder
	var active = sysDepts[0].Status
	for index, value := range sysDepts {
		if index == len(sysDepts) {
			ids.WriteString(strconv.FormatInt(value.Id, 10))
		} else {
			ids.WriteString(strconv.FormatInt(value.Id, 10))
		}
	}
	exec, err := DB.Exec("update sys_dept set status = ? where id in ( ? )", active, ids.String())
	if err != nil {
		panic(err)
		return nil, err
	}
	rowsAffected, err := exec.RowsAffected()
	if err != nil {
		panic(err)
		return nil, err
	}
	return rowsAffected, nil
}
